//
//  ElectSignListViewAlert.m
//  阿帕
//
//  Created by mac on 2020/9/1.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "ElectSignListViewAlert.h"

//static const CGFloat AlertViewWidth = kScreenWidth - 40;
static const CGFloat AlertViewMarginH = 20;
//static const CGFloat AlertViewVerticalElementSpace = 10;
//static const CGFloat AlertViewButtonHeight = 44;
//static const CGFloat AlertViewLineLayerWidth = 0.5;

@interface ElectSignListViewAlert(){
    int timeOld;
    NSTimer *_timer;
}
@property (strong, nonatomic) CJLabel *labTitle;
@property (nonatomic,strong) CJButton *btnAouthCode;
@property (nonatomic,strong) UITextField *fieldAouthCode;
@property (strong, nonatomic) CJButton *btnSure;
@property (strong, nonatomic) CJButton *btnCancle;
@property (nonatomic,copy) OnSureClick sureBlock;
@property (nonatomic,copy) OnCancleClick cancleBlock;
@property (nonatomic,copy) NSString *strCode;
@property (nonatomic,strong) CJView *coverView;

@end

@implementation ElectSignListViewAlert



+ (ElectSignListViewAlert *)shared {
    
    static dispatch_once_t predicate;
    
    static ElectSignListViewAlert *objc;
    
    dispatch_once(&predicate, ^{
        
        objc = [[ElectSignListViewAlert alloc] init];
        
    });
    
    return objc;
}



-(instancetype)initWithTitle:(NSString *)strTitle listTitle:(NSArray *)arrTitle listContent:(NSArray *)arrContent ClickOnSure:(OnSureClick )sureClick ClickOnCancle:(OnCancleClick )cancleClick{
    
    if (self = [super init]) {
        
        //        self.frame = CGRectMake(0, 0, kScreenWidth - 40, 400);
        //        self.backgroundColor = [UIColor cjWhiteColor];
        //        [CJUtil getViewCutAllCornRound:self height:10];
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [[UIColor cjBlackColor] colorWithAlphaComponent:0.3];
        
        [self addSubview:self.coverView];
        self.coverView.center = self.center;
        
        [self.coverView addSubview:self.labTitle];
        self.labTitle.text = strTitle;
        
        self.strCode = arrContent[3];
        CGFloat leftMarg = 10;
        for (int i = 0; i < arrTitle.count; i ++) {
            
            UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(leftMarg, self.labTitle.bottom +  + i * 50, self.coverView.width - leftMarg*2, 40)];
            
            liebiaoView.backgroundColor = [UIColor cjWhiteColor];
            [self.coverView addSubview:liebiaoView];
            
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 80, 40)];
            label.text = arrTitle[i];
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont cjTitleFont14];
            label.textColor = [UIColor cjMainTextColor];
            [liebiaoView addSubview:label];
            
            
            if (i != 3) {
                
                UILabel * contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(label.right, label.top, self.coverView.width -label.width - 25, 40)];
                contentLabel.text = arrContent[i];
                contentLabel.textAlignment = NSTextAlignmentRight;
                contentLabel.font = [UIFont cjTitleFont14];
                contentLabel.textColor = [UIColor cjMainTextColor];
                [liebiaoView addSubview:contentLabel];
                
                UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(0, liebiaoView.height -1, self.width, CJLineHeight)];
                bg.backgroundColor = [UIColor cjLineColor];
                [liebiaoView addSubview:bg];
                
            }else{
                
                CJView *viewAouth = CJView.viewInit
                .viewFrame(CGRectMake(label.right, label.top, self.coverView.width - 0-label.width - 25, 40))
                .viewCornerRadius(CJUICutCorner)
                .viewBorderWidthColor(CJUICornerWidth ,[UIColor cjLineColor])
                ;
                [liebiaoView addSubview:viewAouth];
                
                
                self.fieldAouthCode = [[UITextField alloc]initWithFrame:CGRectMake(10, 0,viewAouth.width - 110 , viewAouth.height)];
                self.fieldAouthCode.font = [UIFont cjTitleFont14];
                self.fieldAouthCode.textColor = [UIColor cjMainTextColor];
                self.fieldAouthCode.placeholder = @"请填写验证码";
                [viewAouth addSubview:self.fieldAouthCode];
                
                self.btnAouthCode = CJButton.btnInit
                .btnFrame(CGRectMake(self.fieldAouthCode.right, 0, viewAouth.width - 20 - self.fieldAouthCode.width, viewAouth.height))
                .btnNormalTitleColor([UIColor cjMainColorText])
                .btnSelectTitleColor([UIColor cjMainColorText])
                .btnText(@"获取验证码")
                .btnTargetAction(self,@selector(btnGetAouthCodeAction:))
                ;
                [viewAouth addSubview:self.btnAouthCode];
                
            }
            
        }
        
        
        [self.coverView addSubview:self.btnCancle];
        [self.coverView addSubview:self.btnSure];
        
        self.sureBlock = sureClick;
        self.cancleBlock = cancleClick;
        
    }
    return self;
}


/// 获取验证码
/// @param sender <#sender description#>
- (void)btnGetAouthCodeAction:(CJButton *)sender{
    
    WEAKSELF
    [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@/elecSign/sendSms",[NSString stringWithFormat:@"%@/ntocc-tps-api",[ReAddressDynamic shared].baseUrl]) withParameter:@{@"code":self.strCode,@"token":[UserDefaultUtil valueForKey:USER_TOKEN]} withVC:[GetVC getCurrentVC] netName:@"电子签章获取短信" success:^(id response, NSString *msg) {
        
        weakSelf.btnAouthCode.userInteractionEnabled = NO;
        timeOld = 60;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireOdlM:) userInfo:nil repeats:weakSelf];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    
}

- (void)timerFireOdlM:(NSTimer *)theTimer{
    
    NSString * str =[NSString stringWithFormat:@"(%ds)",timeOld];
    //    [self.btnGetCode setTitle:str forState:UIControlStateNormal];
    [self.btnAouthCode setTitle:str forState:UIControlStateNormal];
    timeOld--;
    if (timeOld==0) {
        [_timer invalidate];
        _timer = nil;
        timeOld=60;
        [self.btnAouthCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.btnAouthCode setTitle:@"获取验证码" forState:UIControlStateSelected];
        self.btnAouthCode.userInteractionEnabled = YES;
        
    }
}


- (void)btnClickSureAction:(CJButton *)sender{
    
    
    
    if (![CYCheckRegular cy_checkAouthCode:self.fieldAouthCode.text]) {
        [XSInfoView showInfo:@"请填写正确的验证码" onView:self.window];
        
        
    }else{
        
        NSDictionary *dicPra = @{
            @"code":self.strCode,
            @"smsCode":self.fieldAouthCode.text,
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        WEAKSELF
        [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@/elecSign/eSign",[NSString stringWithFormat:@"%@/ntocc-tps-api",[ReAddressDynamic shared].baseUrl]) withParameter:dicPra withVC:[GetVC getCurrentVC] netName:@"电子签章" success:^(id response, NSString *msg) {
            [weakSelf hidden];
            if (weakSelf.sureBlock) {
                weakSelf.sureBlock(@"");
            }
            
            [XSInfoView showInfo:msg onView:self.window];
            
        } failure:^(NSInteger code, NSString *msg) {
            
            [XSInfoView showInfo:msg onView:self.window];
            
        }];
        
    }
    
    
    
    //    WLAlertView * alerView =(WLAlertView *)[self superview];
    //    [alerView dismissAnimated:YES];
    //
    //    if (![CJRegular getDealCode:[CJControl getThreeUnaryValueStr:self.fieldAouthCode.text]]) {
    //
    //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //            [CJAlert showStringDismissWithDelay:@"请填写正确的验证码"];
    //        });
    //
    //    }else{
    //
    //        NSString *strURL = CJStringWithFormat(@"%@/ntocc-tps-api/elecSign/eSign",CJBaseUrl);
    //        NSDictionary *dicPar = @{
    //                                 @"code":self.strCode,
    //                                 @"smsCode":self.fieldAouthCode.text
    //        };
    //
    //        WS(ws);
    //        [[HDNetworking sharedHDNetworking] PUT:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
    //
    //            DLog(@"++++++++%@",responseObject);
    //            if (self.sureBlock) {
    //                self.sureBlock(@"");
    //            }
    //
    //        } failure:^(NSError * _Nonnull error) {
    //            DLog(@"++++++++%@",error);
    //
    //        }];
    //
    //
    //    }
    
    
    //    [alerView dismissAnimated:YES];
}
- (void)btnClickCancleAction:(CJButton *)sender{
    if (self.cancleBlock) {
        
        self.cancleBlock(@"");
    }
    //    WLAlertView * alerView =(WLAlertView *)[self superview];
    //    [alerView dismissAnimated:YES];
    [self hidden];
}





/// 展示隐藏
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    //    _titleLabel.text = _title;
    //    _msgLabel.text = _message;
    self.center = [UIApplication sharedApplication].keyWindow.center;
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)hidden {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0;
    }];
    
    timeOld = 60;
    [_timer invalidate];
    _timer = nil;
}


- (CJView *)coverView{
    if (!_coverView) {
        _coverView =CJView.viewInit
        .viewFrame(CGRectMake(0, 0,kScreenWidth - 40, 380))
        .viewbgColor([UIColor cjWhiteColor])
        ;
        //        _coverView.alpha = 0.3;
    }
    return _coverView;
}


- (CJLabel *)labTitle{
    if (!_labTitle) {
        
        _labTitle = CJLabel.labInit
        .labFrame(CGRectMake(0,0, self.coverView.width, 60))
        //        .labFontSize(18)
        .labFont([UIFont boldSystemFontOfSize:18])
        .labAlignment(NSTextAlignmentCenter)
        .labTextColor(CJRGB16Color(0x444D68))
        .labAdjustsFontSizeToFitWidth(YES)
        ;
    }
    return _labTitle;
}
- (CJButton *)btnCancle{
    
    if (!_btnCancle) {
        _btnCancle = CJButton.btnInit
        .btnFrame(CGRectMake(20, 290, (self.coverView.width - 60)/2,35))
        .btnText(@"取消")
        .btnFontSize(15)
        .btnBgColor([UIColor cjWhiteColor])
        .btnCornerWidth(0.5)
        .btnCornerRadius(5)
        .btnCornerColor([UIColor cjMainColor])
        .btnNormalTitleColor([UIColor cjMainColor])
        .btnTargetAction(self,@selector(btnClickCancleAction:));
    }
    return _btnCancle;
}

- (CJButton *)btnSure{
    
    if (!_btnSure) {
        _btnSure = CJButton.btnInit
        .btnFrame(CGRectMake(self.btnCancle.right + 20, self.btnCancle.top, self.btnCancle.width,self.btnCancle.height ))
        .btnText(@"确定")
        .btnFontSize(15)
        .btnBgColor([UIColor cjMainColor])
        //        .btnCornerWidth(0.5)
        .btnCornerRadius(5)
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnTargetAction(self,@selector(btnClickSureAction:));
    }
    return _btnSure;
}


@end
